<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="js/layer/css/layui.css" rel="stylesheet">
    <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="css/style.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="layui-container ws-container container-fluid">
    <div class="layui-code-preview" style="background: #fff;padding: 2%; ">

        <div class="layui-tab layui-tab-brief" lay-filter="LAY-CODE-DF-1" style="position: relative;">
            <div class="layui-tab-title">
                <li class="layui-this" >header公共变量</li>
            </div>
        </div>

        <div class="layui-code-item layui-code-item-preview layui-border layui-show" style="">
            <form class="layui-form" action="">

                <textarea name="env" id="env" placeholder="请输入内容" class="layui-textarea" style="height: 300px;">
{
    "token": "123456"
}</textarea>
                <br />
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">保存</button>

            </form>
        </div>

    </div>
</div>

<script type="text/javascript" src="js/layer/layui.js"></script>
<script>
    ready(function() {
        layui.use(['form', 'zhanshop'], function(){
            var $ = layui.$;
            var form = layui.form;
            var zhanshop = layui.zhanshop;
            var headerVar = zhanshop.getSesson('header-var');
            if(headerVar != null) $("#env").val(headerVar);
            form.on('submit(demo1)', function(data){
                var field = data.field; // 获取表单字段值
                if(field.env.length == 0){
                    layer.alert("您还没有输入任何内容", {
                        title: '警告'
                    });
                    return false;
                }
                try {
                    var jsonVar = JSON.parse(field.env);
                    zhanshop.setSesson("header-var", field.env);
                    layer.alert("环境变量内容已经保存", {
                        title: '成功'
                    });
                }catch (e){
                    layer.alert("请输入内容存在语法错误", {
                        title: '警告'
                    });
                }
                return false; // 阻止默认 form 跳转
            });
        })
    });
</script>

</body>
</html>